package cn.xiaoxige.plugin

import cn.xiaoxige.plugin.entity.Clazz
import cn.xiaoxige.plugin.entity.Person
import cn.xiaoxige.plugin.task.TeamTask
import cn.xiaoxige.plugin.transform.PrintTransform
import com.android.build.gradle.AppExtension
import org.gradle.BuildListener
import org.gradle.BuildResult
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.ProjectEvaluationListener
import org.gradle.api.ProjectState
import org.gradle.api.initialization.Settings
import org.gradle.api.invocation.Gradle

/*
 * Copyright (C) 2020 Baidu, Inc. All Rights Reserved.
 */

class XiaoxigePlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
        project.extensions.add('person', Person)

        NamedDomainObjectContainer<Person> persons = project.container(Person)
        project.extensions.add('team', persons)

        NamedDomainObjectContainer<Person> clazzPersons = project.container(Person)
        project.extensions.add('clazz', new Clazz(clazzPersons))

        AppExtension appExtension = project.extensions.getByType(AppExtension.class)
        appExtension.registerTransform(new PrintTransform(project))

        project.getTasks().create('teamTask', TeamTask)

        println('--------------------------------------------------')
        def cleanTasks = project.getTasksByName('clean', false)
        println("cleanTask size = ${cleanTasks.size()}")
        cleanTasks.forEach {
            println(it.name)
        }
        println('--------------------------------------------------')

        project.getTasksByName('clean', false)[0].dependsOn('teamTask')

        project.afterEvaluate {
            println('当前配置 person 如下：')
            Person person = project.extensions.getByName('person')
            println(person.toString())

            println('当前配置的 team 如下：')
            NamedDomainObjectContainer<Person> team = project.extensions.getByName('team')
            team.forEach({ item ->
                println(item.toString())
            })

            println('当前配置的 clazz 如下:')
            Clazz clazz = project.extensions.getByName('clazz')
            println(clazz.toString())
        }

        project.gradle.addBuildListener(new BuildListener() {
            @Override
            void buildStarted(Gradle gradle) {

            }

            @Override
            void settingsEvaluated(Settings settings) {

            }

            @Override
            void projectsLoaded(Gradle gradle) {

            }

            @Override
            void projectsEvaluated(Gradle gradle) {

            }

            @Override
            void buildFinished(BuildResult buildResult) {

            }
        })

        project.gradle.addProjectEvaluationListener(new ProjectEvaluationListener() {
            @Override
            void beforeEvaluate(Project p) {

            }

            @Override
            void afterEvaluate(Project p, ProjectState projectState) {

            }
        })
    }
}